package com.lyf.learn.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

/**
 * @AUTHOR LYF
 * @DATE 2022/5/23
 * @VERSION 1.0
 * @DESC
 */
public class JmsConsumer {
    private static final String CONNECT_URL = "tcp://47.98.99.88:61616";
    private static final String QUEUE_NAME1 = "queue-test-1";

    public static void main(String[] args) throws JMSException, IOException {
        ActiveMQConnectionFactory mqConnectionFactory = new ActiveMQConnectionFactory(CONNECT_URL);
        Connection connection = mqConnectionFactory.createConnection();
        // 需主动开启
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Queue queue = session.createQueue(QUEUE_NAME1);
        //MessageConsumer consumer = session.createConsumer(QUEUE_NAME1);
        MessageConsumer messageConsumer = session.createConsumer(queue);

        messageConsumer.setMessageListener(new MessageListener(){
            @Override
            public void onMessage(Message message) {
                if(message!=null&&message instanceof TextMessage){
                    try {
                        System.out.println(((TextMessage) message).getText());
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        System.in.read();
    }
}
